﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using radnaVerzija.Interfejsi;
using MySql.Data.MySqlClient;

namespace radnaVerzija
{
    public partial class DAL
    {
    public  class TerenDAO : IDaoCrud<Teren>
    {
            protected MySqlCommand komanda;

            public long create(Teren entitet)
            {
                try
                {
                    komanda = new MySqlCommand("insert Tereni values ('" +
                        entitet.TipTerena + "', '" + entitet.Nadzornik.Ime + "', '" +  entitet.Nadzornik.Prezime + "', '" +
                        entitet.DaLiJeIspravan.ToString() +
                        ";", konekcija);
                    komanda.ExecuteNonQuery();
                    return komanda.LastInsertedId;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Teren read(Teren entitet)
            {
                try
                {
                    komanda = new MySqlCommand("select * from Tereni where teren_ID = '" + entitet.IdTerena.ToString() + "';", konekcija);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    Teren teren = null;
                    while (citac.Read())
                    {
                        
                        teren = new Teren(citac.GetString("tip terena"), citac.GetBoolean("ispravnost"));
                        return teren;
                    }
                    return teren;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Teren update(Teren entitet)
            {
                try
                {
                    komanda = new MySqlCommand("update Tereni set tip terena = '" + entitet.TipTerena +
                        "', ispravnost ='" + entitet.DaLiJeIspravan.ToString() +
                        "', nadzornik =" + (entitet.Nadzornik.Ime+" "+entitet.Nadzornik.Prezime)  +
                        " where teren_ID = " + entitet.IdTerena, konekcija);
                    komanda.ExecuteNonQuery();
                    return read(entitet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public void delete(Teren entitet)
            {
                try
                {
                    komanda = new MySqlCommand("delete from Tereni where teren_ID = " + entitet.IdTerena + ";", konekcija);
                    komanda.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }
}
